<!doctype html><html><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=robots content="noodp"><meta http-equiv=x-ua-compatible content="IE=edge, chrome=1"><title>Spring Data MongoDB问题汇总 - Yulin Lewis' Blog</title><meta name=keywords content="雨临Lewis,Java,hugo,hexo,博客"><meta name=Description content="不想当写手的码农不是好咸鱼_(xз」∠)_"><meta property="og:title" content="Spring Data MongoDB问题汇总"><meta property="og:description" content="前言
Spring Data除了常用的JPA（Hibernate）关系型数据库的模块外，还有其他用于非关系型数据库的数据交互模块：比如Redis、MongoDB、Elasticsearch等。
用法和JPA模块类似，都需要定义对应的POJO、Repository，同时也提供了对应的数据库工具模板类：如RedisTemplate、MongoTemplate等。
本文基于以下版本：


1
2
3
4
5
6


<!--  对应的是4.1.1版本的MongoDB -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
    <version>2.4.0</version>
</dependency>


这是MongoDB官网用户手册的翻译文档仓库：MongoDB-4.2-Manual"><meta property="og:type" content="article"><meta property="og:url" content="https://lewky.cn/posts/spring-data-mongodb/"><meta property="og:image" content="https://lewky.cn/logo.png"><meta property="article:published_time" content="2021-09-27T23:27:22+08:00"><meta property="article:modified_time" content="2021-09-27T23:27:22+08:00"><meta name=twitter:card content="summary_large_image"><meta name=twitter:image content="https://lewky.cn/logo.png"><meta name=twitter:title content="Spring Data MongoDB问题汇总"><meta name=twitter:description content="前言
Spring Data除了常用的JPA（Hibernate）关系型数据库的模块外，还有其他用于非关系型数据库的数据交互模块：比如Redis、MongoDB、Elasticsearch等。
用法和JPA模块类似，都需要定义对应的POJO、Repository，同时也提供了对应的数据库工具模板类：如RedisTemplate、MongoTemplate等。
本文基于以下版本：


1
2
3
4
5
6


<!--  对应的是4.1.1版本的MongoDB -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
    <version>2.4.0</version>
</dependency>


这是MongoDB官网用户手册的翻译文档仓库：MongoDB-4.2-Manual"><meta name=application-name content="雨临Lewis的博客"><meta name=apple-mobile-web-app-title content="雨临Lewis的博客"><meta name=theme-color content="#ffffff"><meta name=msapplication-TileColor content="#da532c"><link rel="shortcut icon" type=image/x-icon href=/favicon.ico><link rel=icon type=image/png sizes=32x32 href=/favicon-32x32.png><link rel=icon type=image/png sizes=16x16 href=/favicon-16x16.png><link rel=apple-touch-icon sizes=180x180 href=/apple-touch-icon.png><link rel=mask-icon href=/safari-pinned-tab.svg color=#5bbad5><link rel=manifest href=/site.webmanifest><link rel=canonical href=https://lewky.cn/posts/spring-data-mongodb/><link rel=prev href=https://lewky.cn/posts/log-framework/><link rel=next href=https://lewky.cn/posts/mysql-issues/><link rel=stylesheet href=https://cdn.jsdelivr.net/npm/normalize.css@8.0.1/normalize.min.css><link rel=stylesheet href=/css/style.min.css><link rel=stylesheet href=https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.13.0/css/all.min.css><link rel=stylesheet href=https://cdn.jsdelivr.net/npm/animate.css@3.7.2/animate.min.css><script type=application/ld+json>{"@context":"http://schema.org","@type":"BlogPosting","headline":"Spring Data MongoDB问题汇总","inLanguage":"","mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/lewky.cn\/posts\/spring-data-mongodb\/"},"genre":"posts","keywords":"工作记录, Spring, MongoDB","wordcount":2011,"url":"https:\/\/lewky.cn\/posts\/spring-data-mongodb\/","datePublished":"2021-09-27T23:27:22+08:00","dateModified":"2021-09-27T23:27:22+08:00","license":"This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher":{"@type":"Organization","name":"雨临Lewis","logo":"https:\/\/lewky.cn\/images\/avatar.jpg"},"author":{"@type":"Person","name":"雨临Lewis"},"description":""}</script></head><body header-desktop=fixed header-mobile=auto><script type=text/javascript>(window.localStorage&&localStorage.getItem('theme')?localStorage.getItem('theme')==='dark':('auto'==='auto'?window.matchMedia('(prefers-color-scheme: dark)').matches:'auto'==='dark'))&&document.body.setAttribute('theme','dark');</script><div id=mask></div><div class=wrapper><header class=desktop id=header-desktop><div class=header-wrapper><a href=https://github.com/lewky class=github-corner target=_blank title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="3.5rem" height="3.5rem" viewBox="0 0 250 250" style="fill:#70b7fd;color:#fff;position:absolute;top:0;border:0;left:0;transform:scale(-1,1)" aria-hidden="true"><path d="M0 0 115 115h15l12 27L250 250V0z"/><path d="M128.3 109C113.8 99.7 119 89.6 119 89.6 122 82.7 120.5 78.6 120.5 78.6 119.2 72 123.4 76.3 123.4 76.3 127.3 80.9 125.5 87.3 125.5 87.3 122.9 97.6 130.6 101.9 134.4 103.2" fill="currentcolor" style="transform-origin:130px 106px" class="octo-arm"/><path d="M115 115C114.9 115.1 118.7 116.5 119.8 115.4L133.7 101.6C136.9 99.2 139.9 98.4 142.2 98.6 133.8 88 127.5 74.4 143.8 58 148.5 53.4 154 51.2 159.7 51 160.3 49.4 163.2 43.6 171.4 40.1 171.4 40.1 176.1 42.5 178.8 56.2 183.1 58.6 187.2 61.8 190.9 65.4 194.5 69 197.7 73.2 200.1 77.6 213.8 80.2 216.3 84.9 216.3 84.9 212.7 93.1 206.9 96 205.4 96.6 205.1 102.4 203 107.8 198.3 112.5 181.9 128.9 168.3 122.5 157.7 114.1 157.9 116.9 156.7 120.9 152.7 124.9L141 136.5C139.8 137.7 141.6 141.9 141.8 141.8z" fill="currentcolor" class="octo-body"/></svg></a><div class=header-title><a href=/ title="Yulin Lewis' Blog"><span class=header-title-pre><i class="fas fa-fw fa-atom"></i></span>雨临Lewis</a></div><div class=menu><div class=menu-inner><div class=dropdown><a href=/posts/ class="menu-item menu-more dropbtn" title=点击查看所有文章><i class="fas fa-fw fa-archive"></i>归档</a><div class="menu-more-content dropdown-content"><a href=/categories/><i class="fas fa-fw fa-th"></i>分类 </a><a href=/tags/><i class="fas fa-fw fa-tag"></i>标签 </a><a href=/hot/ title=文章热度Top15><i class="fas fa-fw fa-fire"></i>热度 </a><a href=/donation/ title=感谢打赏，老板大气~><i class="fas fa-fw fa-donate"></i>打赏 </a><a href=/about/><i class="fas fa-fw fa-at"></i>关于</a></div></div><a class=menu-item href=/friends/ title=欢迎申请友链><i class="fas fa-fw fa-link"></i>友链</a><div class=dropdown><a href=javascript:void(0); class="menu-item menu-more dropbtn" title=一些有意思的东东~><i class="fas fa-fw fa-fan fa-spin"></i>趣味</a><div class="menu-more-content dropdown-content"><a href=/funny/high/ title="前方高能♂ 小心外放！" target=_blank rel=noopener><i class="fas fa-fw fa-dizzy"></i>燥起来！ </a><a href=/funny/mikutap/ title=初音未来音乐游戏 target=_blank rel=noopener><i class="fas fa-fw fa-music"></i>在线打碟 </a><a href=/funny/catch-the-cat/ title=逮住那只猫!><i class="fas fa-fw fa-cat"></i>抓住猫咪</a></div></div><div class=dropdown><a href=/ class="menu-item menu-more dropbtn" title=回到首页><i class="fas fa-fw fa-atom"></i>站点</a><div class="menu-more-content dropdown-content"><a href=/bbs/ title=来留言吧~><i class="fas fa-fw fa-comment"></i>公告留言 </a><a href=https://seo.chinaz.com target=_blank rel=noopener><i class="fas fa-fw fa-chart-line"></i>SEO查询 </a><a href=https://www.ping.cn/http/lewky.cn target=_blank rel=noopener><i class="fas fa-fw fa-bezier-curve"></i>网站测速 </a><a href=/posts/e62c38c4.html/><i class="fas fa-fw fa-cog fa-spin"></i>建站日志</a></div></div><div class=dropdown><a href=javascript:void(0); class="menu-item menu-more dropbtn"><i class="fas fa-fw fa-book"></i>文档</a><div class="menu-more-content dropdown-content"><a href=https://gohugo.io/documentation/ target=_blank rel=noopener><i class="fas fa-fw fa-star"></i>Hugo文档 </a><a href=https://javanote.doc.lewky.cn/ title=尚在完善中~ target=_blank rel=noopener><i class="fab fa-fw fa-java"></i>Java 笔记</a></div></div><a class=menu-item href=/search/ title=渲染搜索结果需要等待几秒钟~><i class="fas fa-fw fa-search"></i>搜索
</a><span class="menu-item delimiter"></span><a href=https://travellings.link target=_blank class=menu-item rel=noopener title=开往-友链接力><i class="fas fa-fw fa-subway"></i></a><a href=https://rssblog.vercel.app/ class=menu-item target=_blank rel=noopener title=RSSBlog><i class="fas fa-fw fa-inbox"></i></a><a href=https://github.com/lewky class=menu-item target=_blank rel=noopener title=GitHub><i class="fab fa-fw fa-github"></i></a></div></div></div></header><header class=mobile id=header-mobile><div class=header-container><div class=header-wrapper><div class=header-title><a href=/ title="Yulin Lewis' Blog"><span class=header-title-pre><i class="fas fa-fw fa-atom"></i></span>雨临Lewis</a></div><div class=menu-toggle id=menu-toggle-mobile><span></span><span></span><span></span></div></div><div class=menu id=menu-mobile><div class=dropdown><a href=/posts/ class="menu-item menu-more dropbtn" title=点击查看所有文章><i class="fas fa-fw fa-archive"></i>归档</a><div class="menu-more-content dropdown-content"><a href=/categories/><i class="fas fa-fw fa-th"></i>分类 </a><a href=/tags/><i class="fas fa-fw fa-tag"></i>标签 </a><a href=/hot/ title=文章热度Top15><i class="fas fa-fw fa-fire"></i>热度 </a><a href=/donation/ title=感谢打赏，老板大气~><i class="fas fa-fw fa-donate"></i>打赏 </a><a href=/about/><i class="fas fa-fw fa-at"></i>关于</a></div></div><a class=menu-item href=/friends/ title=欢迎申请友链><i class="fas fa-fw fa-link"></i>友链</a><div class=dropdown><a href=javascript:void(0); class="menu-item menu-more dropbtn" title=一些有意思的东东~><i class="fas fa-fw fa-fan fa-spin"></i>趣味</a><div class="menu-more-content dropdown-content"><a href=/funny/high/ title="前方高能♂ 小心外放！" target=_blank rel=noopener><i class="fas fa-fw fa-dizzy"></i>燥起来！ </a><a href=/funny/mikutap/ title=初音未来音乐游戏 target=_blank rel=noopener><i class="fas fa-fw fa-music"></i>在线打碟 </a><a href=/funny/catch-the-cat/ title=逮住那只猫!><i class="fas fa-fw fa-cat"></i>抓住猫咪</a></div></div><div class=dropdown><a href=/ class="menu-item menu-more dropbtn" title=回到首页><i class="fas fa-fw fa-atom"></i>站点</a><div class="menu-more-content dropdown-content"><a href=/bbs/ title=来留言吧~><i class="fas fa-fw fa-comment"></i>公告留言 </a><a href=https://seo.chinaz.com target=_blank rel=noopener><i class="fas fa-fw fa-chart-line"></i>SEO查询 </a><a href=https://www.ping.cn/http/lewky.cn target=_blank rel=noopener><i class="fas fa-fw fa-bezier-curve"></i>网站测速 </a><a href=/posts/e62c38c4.html/><i class="fas fa-fw fa-cog fa-spin"></i>建站日志</a></div></div><div class=dropdown><a href=javascript:void(0); class="menu-item menu-more dropbtn"><i class="fas fa-fw fa-book"></i>文档</a><div class="menu-more-content dropdown-content"><a href=https://gohugo.io/documentation/ target=_blank rel=noopener><i class="fas fa-fw fa-star"></i>Hugo文档 </a><a href=https://javanote.doc.lewky.cn/ title=尚在完善中~ target=_blank rel=noopener><i class="fab fa-fw fa-java"></i>Java 笔记</a></div></div><a class=menu-item href=/search/ title=渲染搜索结果需要等待几秒钟~><i class="fas fa-fw fa-search"></i>搜索</a>
<a href=https://travellings.link target=_blank class=menu-item rel=noopener title=开往-友链接力><i class="fas fa-fw fa-subway"></i></a><a href=https://rssblog.vercel.app/ class=menu-item target=_blank rel=noopener title=RSSBlog><i class="fas fa-fw fa-inbox"></i></a><a href=https://github.com/lewky class=menu-item target=_blank rel=noopener title=GitHub><i class="fab fa-fw fa-github"></i></a></div></div></header><div class="search-dropdown desktop"><div id=search-dropdown-desktop></div></div><div class="search-dropdown mobile"><div id=search-dropdown-mobile></div></div><main class=main><div class=container><div class=toc id=toc-auto><h2 class=toc-title>目录</h2><div class=toc-content id=toc-content-auto></div></div><article class="page single"><h1 class="single-title animated flipInX">Spring Data MongoDB问题汇总</h1><div class=post-meta><div class=post-meta-line><span class=post-author><a href=/ title=Author rel=author class=author><i class="fas fa-user-circle fa-fw"></i>雨临Lewis</a></span>&nbsp;<span class=post-category>收录于 <a href=/categories/%E5%B7%A5%E4%BD%9C%E8%AE%B0%E5%BD%95/><i class="far fa-folder fa-fw"></i>工作记录</a></span></div><div class=post-meta-line><i class="far fa-calendar fa-fw"></i>&nbsp;<time datetime=2021-09-27>2021-09-27</time>&nbsp;<i class="far fa-calendar-plus fa-fw"></i>&nbsp;<time datetime=2021-09-27>2021-09-27</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 2011 字
<i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 5 分钟&nbsp;<span id=/posts/spring-data-mongodb/ class=leancloud_visitors data-flag-title="Spring Data MongoDB问题汇总">
<i class="far fa-eye fa-fw"></i>&nbsp;<span class=leancloud-visitors-count></span>&nbsp;次阅读
</span>&nbsp;
<a href=#comments id=post-meta-vcount title=查看评论><i class="fas fa-comment fa-fw"></i>&nbsp;<span id=/posts/spring-data-mongodb/ class=waline-comment-count></span>&nbsp;条评论</a></div></div><div class="details toc" id=toc-static kept><div class="details-summary toc-title"><span>目录</span>
<span><i class="details-icon fas fa-angle-right"></i></span></div><div class="details-content toc-content" id=toc-content-static><nav id=TableOfContents><ol><li><a href=#前言>前言</a></li><li><a href=#忽略某个字段>忽略某个字段</a></li><li><a href=#移除_class字段>移除_class字段</a></li><li><a href=#不支持zoneddatetime类型>不支持ZonedDateTime类型</a></li><li><a href=#the-bean-xxx-defined-in-null-could-not-be-registered>The bean ‘xxx’, defined in null, could not be registered.</a></li><li><a href=#整合多个数据库>整合多个数据库</a></li><li><a href=#使用spel表达式来动态获取集合的值>使用SPEL表达式来动态获取集合的值</a></li><li><a href=#查询数据库>查询数据库</a></li><li><a href=#参考链接>参考链接</a></li></ol></nav></div></div><div class=content id=content><h2 id=前言>前言</h2><p>Spring Data除了常用的JPA（Hibernate）关系型数据库的模块外，还有其他用于非关系型数据库的数据交互模块：比如Redis、MongoDB、Elasticsearch等。</p><p>用法和JPA模块类似，都需要定义对应的POJO、Repository，同时也提供了对应的数据库工具模板类：如RedisTemplate、MongoTemplate等。</p><p>本文基于以下版本：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span><span class=lnt>3
</span><span class=lnt>4
</span><span class=lnt>5
</span><span class=lnt>6
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>&lt;!--  对应的是4.1.1版本的MongoDB --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt;
    &lt;version&gt;2.4.0&lt;/version&gt;
&lt;/dependency&gt;
</code></pre></td></tr></table></div></div><p>这是MongoDB官网用户手册的翻译文档仓库：<a href=https://github.com/mongodb-china/MongoDB-4.2-Manual target=_blank rel="noopener noreffer">MongoDB-4.2-Manual</a></p><h2 id=忽略某个字段>忽略某个字段</h2><p>和JPA-Hibernate类似，使用<code>@Transient</code>即可。注意不能使用<code>javax.persistence.Transient</code>，这个是JPA规范的注解，对Spring Data MongoDB无效，需要使用<code>org.springframework.data.annotation.Transient</code>。</p><h2 id=移除_class字段>移除_class字段</h2><p>Spring Data在查询MongoDB时会自动添加<code>_class</code>字段，可以用以下方式移除：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt> 1
</span><span class=lnt> 2
</span><span class=lnt> 3
</span><span class=lnt> 4
</span><span class=lnt> 5
</span><span class=lnt> 6
</span><span class=lnt> 7
</span><span class=lnt> 8
</span><span class=lnt> 9
</span><span class=lnt>10
</span><span class=lnt>11
</span><span class=lnt>12
</span><span class=lnt>13
</span><span class=lnt>14
</span><span class=lnt>15
</span><span class=lnt>16
</span><span class=lnt>17
</span><span class=lnt>18
</span><span class=lnt>19
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-java data-lang=java>
<span class=nd>@Configuration</span>
<span class=kd>public</span> <span class=kd>class</span> <span class=nc>MongoDBConfig</span> <span class=o>{</span>

    <span class=nd>@Bean</span>
    <span class=kd>public</span> <span class=n>MappingMongoConverter</span> <span class=nf>mappingMongoConverter</span><span class=o>(</span><span class=n>MongoDbFactory</span> <span class=n>factory</span><span class=o>,</span> <span class=n>MongoMappingContext</span> <span class=n>context</span><span class=o>,</span> <span class=n>BeanFactory</span> <span class=n>beanFactory</span><span class=o>)</span> <span class=o>{</span>
        <span class=n>DbRefResolver</span> <span class=n>dbRefResolver</span> <span class=o>=</span> <span class=k>new</span> <span class=n>DefaultDbRefResolver</span><span class=o>(</span><span class=n>factory</span><span class=o>);</span>
        <span class=n>MappingMongoConverter</span> <span class=n>mappingConverter</span> <span class=o>=</span> <span class=k>new</span> <span class=n>MappingMongoConverter</span><span class=o>(</span><span class=n>dbRefResolver</span><span class=o>,</span> <span class=n>context</span><span class=o>);</span>
        <span class=k>try</span> <span class=o>{</span>
            <span class=n>mappingConverter</span><span class=o>.</span><span class=na>setCustomConversions</span><span class=o>(</span><span class=n>beanFactory</span><span class=o>.</span><span class=na>getBean</span><span class=o>(</span><span class=n>CustomConversions</span><span class=o>.</span><span class=na>class</span><span class=o>));</span>
        <span class=o>}</span> <span class=k>catch</span> <span class=o>(</span><span class=n>NoSuchBeanDefinitionException</span> <span class=n>ignore</span><span class=o>)</span> <span class=o>{</span>
 
        <span class=o>}</span>
        
        <span class=c1>// 取消_class字段
</span><span class=c1></span>        <span class=n>mappingConverter</span><span class=o>.</span><span class=na>setTypeMapper</span><span class=o>(</span><span class=k>new</span> <span class=n>DefaultMongoTypeMapper</span><span class=o>(</span><span class=kc>null</span><span class=o>));</span>
        <span class=k>return</span> <span class=n>mappingConverter</span><span class=o>;</span>
    <span class=o>}</span>
<span class=o>}</span>
</code></pre></td></tr></table></div></div><h2 id=不支持zoneddatetime类型>不支持ZonedDateTime类型</h2><p>MongoDB不支持ZonedDateTime，因此在读取和写入时需要转换为<code>java.util.Date</code>或LocalDateTime类型：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt> 1
</span><span class=lnt> 2
</span><span class=lnt> 3
</span><span class=lnt> 4
</span><span class=lnt> 5
</span><span class=lnt> 6
</span><span class=lnt> 7
</span><span class=lnt> 8
</span><span class=lnt> 9
</span><span class=lnt>10
</span><span class=lnt>11
</span><span class=lnt>12
</span><span class=lnt>13
</span><span class=lnt>14
</span><span class=lnt>15
</span><span class=lnt>16
</span><span class=lnt>17
</span><span class=lnt>18
</span><span class=lnt>19
</span><span class=lnt>20
</span><span class=lnt>21
</span><span class=lnt>22
</span><span class=lnt>23
</span><span class=lnt>24
</span><span class=lnt>25
</span><span class=lnt>26
</span><span class=lnt>27
</span><span class=lnt>28
</span><span class=lnt>29
</span><span class=lnt>30
</span><span class=lnt>31
</span><span class=lnt>32
</span><span class=lnt>33
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-java data-lang=java><span class=nd>@Configuration</span>
<span class=kd>public</span> <span class=kd>class</span> <span class=nc>MongoDBConfig</span> <span class=o>{</span>
    <span class=nd>@Autowired</span>
    <span class=n>MongoDbFactory</span> <span class=n>mongoDbFactory</span><span class=o>;</span>
    <span class=nd>@Bean</span>
    <span class=kd>public</span> <span class=n>MongoTemplate</span> <span class=nf>mongoTemplate</span><span class=o>()</span> <span class=kd>throws</span> <span class=n>UnknownHostException</span> <span class=o>{</span>
        <span class=n>MappingMongoConverter</span> <span class=n>converter</span> <span class=o>=</span> <span class=k>new</span> <span class=n>MappingMongoConverter</span><span class=o>(</span><span class=k>new</span> <span class=n>DefaultDbRefResolver</span><span class=o>(</span><span class=n>mongoDbFactory</span><span class=o>),</span>
                <span class=k>new</span> <span class=n>MongoMappingContext</span><span class=o>());</span>
        <span class=n>converter</span><span class=o>.</span><span class=na>setCustomConversions</span><span class=o>(</span><span class=n>customConversions</span><span class=o>());</span>
        <span class=n>converter</span><span class=o>.</span><span class=na>afterPropertiesSet</span><span class=o>();</span>
        <span class=k>return</span> <span class=k>new</span> <span class=n>MongoTemplate</span><span class=o>(</span><span class=n>mongoDbFactory</span><span class=o>,</span> <span class=n>converter</span><span class=o>);</span>
    <span class=o>}</span>
    <span class=kd>public</span> <span class=n>MongoCustomConversions</span> <span class=nf>customConversions</span><span class=o>()</span> <span class=o>{</span>
        <span class=n>List</span><span class=o>&lt;</span><span class=n>Converter</span><span class=o>&lt;?,</span> <span class=o>?&gt;&gt;</span> <span class=n>converters</span> <span class=o>=</span> <span class=k>new</span> <span class=n>ArrayList</span><span class=o>&lt;&gt;();</span>
        <span class=n>converters</span><span class=o>.</span><span class=na>add</span><span class=o>(</span><span class=n>DateToZonedDateTimeConverter</span><span class=o>.</span><span class=na>INSTANCE</span><span class=o>);</span>
        <span class=n>converters</span><span class=o>.</span><span class=na>add</span><span class=o>(</span><span class=n>ZonedDateTimeToDateConverter</span><span class=o>.</span><span class=na>INSTANCE</span><span class=o>);</span>
        <span class=k>return</span> <span class=k>new</span> <span class=n>MongoCustomConversions</span><span class=o>(</span><span class=n>converters</span><span class=o>);</span>
    <span class=o>}</span>
    <span class=nd>@ReadingConverter</span>
    <span class=kd>enum</span> <span class=n>DateToZonedDateTimeConverter</span> <span class=kd>implements</span> <span class=n>Converter</span><span class=o>&lt;</span><span class=n>Date</span><span class=o>,</span> <span class=n>ZonedDateTime</span><span class=o>&gt;</span> <span class=o>{</span>
        <span class=n>INSTANCE</span><span class=o>;</span>
        <span class=kd>public</span> <span class=n>ZonedDateTime</span> <span class=nf>convert</span><span class=o>(</span><span class=n>Date</span> <span class=n>source</span><span class=o>)</span> <span class=o>{</span>
            <span class=k>return</span> <span class=n>source</span> <span class=o>==</span> <span class=kc>null</span> <span class=o>?</span> <span class=kc>null</span> <span class=o>:</span> <span class=n>ZonedDateTime</span><span class=o>.</span><span class=na>ofInstant</span><span class=o>(</span><span class=n>source</span><span class=o>.</span><span class=na>toInstant</span><span class=o>(),</span> <span class=n>ZoneId</span><span class=o>.</span><span class=na>systemDefault</span><span class=o>());</span>
        <span class=o>}</span>
    <span class=o>}</span>
    <span class=nd>@WritingConverter</span>
    <span class=kd>enum</span> <span class=n>ZonedDateTimeToDateConverter</span> <span class=kd>implements</span> <span class=n>Converter</span><span class=o>&lt;</span><span class=n>ZonedDateTime</span><span class=o>,</span> <span class=n>LocalDateTime</span><span class=o>&gt;</span> <span class=o>{</span>
        <span class=n>INSTANCE</span><span class=o>;</span>
        <span class=kd>public</span> <span class=n>LocalDateTime</span> <span class=nf>convert</span><span class=o>(</span><span class=n>ZonedDateTime</span> <span class=n>source</span><span class=o>)</span> <span class=o>{</span>
            <span class=k>return</span> <span class=n>source</span> <span class=o>==</span> <span class=kc>null</span> <span class=o>?</span> <span class=kc>null</span> <span class=o>:</span> <span class=n>LocalDateTime</span><span class=o>.</span><span class=na>ofInstant</span><span class=o>(</span><span class=n>source</span><span class=o>.</span><span class=na>toInstant</span><span class=o>(),</span> <span class=n>ZoneId</span><span class=o>.</span><span class=na>systemDefault</span><span class=o>());</span>
        <span class=o>}</span>
    <span class=o>}</span>
<span class=o>}</span>
</code></pre></td></tr></table></div></div><h2 id=the-bean-xxx-defined-in-null-could-not-be-registered>The bean ‘xxx’, defined in null, could not be registered.</h2><p>当同时使用了多个Spring Data模块时，比如混用了Spring Data JPA和Spring Data MongoDB时就会报这种错：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span><span class=lnt>3
</span><span class=lnt>4
</span><span class=lnt>5
</span><span class=lnt>6
</span><span class=lnt>7
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>Description:
 
The bean &#39;itemMongoRepository&#39;, defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.
 
Action:
 
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
</code></pre></td></tr></table></div></div><p>原因很简单，这些Spring Data模块属于不同的jar，但用的是同一个接口，Spring在运行时不知道当前的bean是绑定的JPA的，还是MongoDB或者Elasticsearch的库。</p><p>此时需要使用注解来声明不同模块对应的包路径，以此区分开这些Repository的bean：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt> 1
</span><span class=lnt> 2
</span><span class=lnt> 3
</span><span class=lnt> 4
</span><span class=lnt> 5
</span><span class=lnt> 6
</span><span class=lnt> 7
</span><span class=lnt> 8
</span><span class=lnt> 9
</span><span class=lnt>10
</span><span class=lnt>11
</span><span class=lnt>12
</span><span class=lnt>13
</span><span class=lnt>14
</span><span class=lnt>15
</span><span class=lnt>16
</span><span class=lnt>17
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-java data-lang=java><span class=nd>@Configuration</span>
<span class=nd>@EnableMongoRepositories</span><span class=o>(</span><span class=n>basePackages</span> <span class=o>=</span> <span class=s>&#34;test.repository.mongodb&#34;</span><span class=o>)</span>
<span class=kd>public</span> <span class=kd>class</span> <span class=nc>MongoConfig</span> <span class=o>{</span>

<span class=o>}</span>

<span class=nd>@Configuration</span>
<span class=nd>@EnableJpaRepositories</span><span class=o>(</span><span class=n>basePackages</span> <span class=o>=</span> <span class=s>&#34;test.repository.jpa&#34;</span><span class=o>)</span>
<span class=kd>public</span> <span class=kd>class</span> <span class=nc>EntityConfig</span> <span class=o>{</span>

<span class=o>}</span>

<span class=nd>@Configuration</span>
<span class=nd>@EnableElasticsearchRepositories</span><span class=o>(</span><span class=n>basePackages</span> <span class=o>=</span> <span class=s>&#34;test.repository.es&#34;</span><span class=o>)</span>
<span class=kd>public</span> <span class=kd>class</span> <span class=nc>ElasticSearchConfig</span> <span class=o>{</span>

<span class=o>}</span>
</code></pre></td></tr></table></div></div><h2 id=整合多个数据库>整合多个数据库</h2><p>现在有两个不同的功能模块，各自对应一个MongoDB，此时需要配置两个不同的数据库配置，并指定不同的MongoTemplate，然后通过调用不同的MongoTemplate来操作不同的MongoDB。</p><p>比如在配置文件中有如下两个数据库：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span><span class=lnt>3
</span><span class=lnt>4
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>## Default MongoDB database
spring.data.mongodb.primary.uri=mongodb://localhost:27017/db1
## Secondary MongoDB database
spring.data.mongodb.secondary.uri=mongodb://localhost:27017/db2
</code></pre></td></tr></table></div></div><p>此时定义两个MongoConfig的bean，各自对应上述两个不同的数据库。由于定义重复了相同类型的bean对象，需要用<code>@Primary</code>来指明默认注入哪个bean对象。</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt> 1
</span><span class=lnt> 2
</span><span class=lnt> 3
</span><span class=lnt> 4
</span><span class=lnt> 5
</span><span class=lnt> 6
</span><span class=lnt> 7
</span><span class=lnt> 8
</span><span class=lnt> 9
</span><span class=lnt>10
</span><span class=lnt>11
</span><span class=lnt>12
</span><span class=lnt>13
</span><span class=lnt>14
</span><span class=lnt>15
</span><span class=lnt>16
</span><span class=lnt>17
</span><span class=lnt>18
</span><span class=lnt>19
</span><span class=lnt>20
</span><span class=lnt>21
</span><span class=lnt>22
</span><span class=lnt>23
</span><span class=lnt>24
</span><span class=lnt>25
</span><span class=lnt>26
</span><span class=lnt>27
</span><span class=lnt>28
</span><span class=lnt>29
</span><span class=lnt>30
</span><span class=lnt>31
</span><span class=lnt>32
</span><span class=lnt>33
</span><span class=lnt>34
</span><span class=lnt>35
</span><span class=lnt>36
</span><span class=lnt>37
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-java data-lang=java><span class=nd>@Configuration</span>
<span class=nd>@EnableMongoRepositories</span><span class=o>(</span><span class=n>basePackages</span> <span class=o>=</span> <span class=s>&#34;test.repository.mongodb.primary&#34;</span><span class=o>,</span>
        <span class=n>mongoTemplateRef</span> <span class=o>=</span> <span class=s>&#34;primaryMongoTemplate&#34;</span><span class=o>)</span>
<span class=kd>public</span> <span class=kd>class</span> <span class=nc>PrimaryMongoConfig</span> <span class=o>{</span>

    <span class=kd>private</span> <span class=kd>static</span> <span class=kd>final</span> <span class=n>String</span> <span class=n>ENTITY_MONGODB_URL</span> <span class=o>=</span> <span class=s>&#34;spring.data.mongodb.primary.uri&#34;</span><span class=o>;</span>

    <span class=nd>@Bean</span><span class=o>(</span><span class=n>name</span> <span class=o>=</span> <span class=s>&#34;primaryMongoTemplate&#34;</span><span class=o>)</span>
    <span class=nd>@Primary</span>
    <span class=kd>public</span> <span class=n>MongoTemplate</span> <span class=nf>mongoTemplate</span><span class=o>(</span><span class=n>Environment</span> <span class=n>env</span><span class=o>)</span> <span class=o>{</span>
        <span class=k>return</span> <span class=k>new</span> <span class=n>MongoTemplate</span><span class=o>(</span><span class=n>mongoFactory</span><span class=o>(</span><span class=n>env</span><span class=o>));</span>
    <span class=o>}</span>

    <span class=nd>@Bean</span><span class=o>(</span><span class=n>name</span> <span class=o>=</span> <span class=s>&#34;primaryMongoFactory&#34;</span><span class=o>)</span>
    <span class=nd>@Primary</span>
    <span class=kd>public</span> <span class=n>MongoDatabaseFactory</span> <span class=nf>mongoFactory</span><span class=o>(</span><span class=n>Environment</span> <span class=n>env</span><span class=o>)</span> <span class=o>{</span>
        <span class=k>return</span> <span class=k>new</span> <span class=n>SimpleMongoClientDatabaseFactory</span><span class=o>(</span><span class=n>env</span><span class=o>.</span><span class=na>getProperty</span><span class=o>(</span><span class=n>ENTITY_MONGODB_URL</span><span class=o>));</span>
    <span class=o>}</span>
<span class=o>}</span>

<span class=nd>@Configuration</span>
<span class=nd>@EnableMongoRepositories</span><span class=o>(</span><span class=n>basePackages</span> <span class=o>=</span> <span class=s>&#34;test.repository.mongodb.secondary&#34;</span><span class=o>,</span>
        <span class=n>mongoTemplateRef</span> <span class=o>=</span> <span class=s>&#34;secondaryMongoTemplate&#34;</span><span class=o>)</span>
<span class=kd>public</span> <span class=kd>class</span> <span class=nc>SecondaryMongoConfig</span> <span class=o>{</span>

    <span class=kd>private</span> <span class=kd>static</span> <span class=kd>final</span> <span class=n>String</span> <span class=n>ENTITY_MONGODB_URL</span> <span class=o>=</span> <span class=s>&#34;spring.data.mongodb.secondary.uri&#34;</span><span class=o>;</span>

    <span class=nd>@Bean</span><span class=o>(</span><span class=n>name</span> <span class=o>=</span> <span class=s>&#34;secondaryMongoTemplate&#34;</span><span class=o>)</span>
    <span class=kd>public</span> <span class=n>MongoTemplate</span> <span class=nf>mongoTemplate</span><span class=o>(</span><span class=n>Environment</span> <span class=n>env</span><span class=o>)</span> <span class=o>{</span>
        <span class=k>return</span> <span class=k>new</span> <span class=n>MongoTemplate</span><span class=o>(</span><span class=n>mongoFactory</span><span class=o>(</span><span class=n>env</span><span class=o>));</span>
    <span class=o>}</span>

    <span class=nd>@Bean</span><span class=o>(</span><span class=n>name</span> <span class=o>=</span> <span class=s>&#34;secondaryMongoFactory&#34;</span><span class=o>)</span>
    <span class=kd>public</span> <span class=n>MongoDatabaseFactory</span> <span class=nf>mongoFactory</span><span class=o>(</span><span class=n>Environment</span> <span class=n>env</span><span class=o>)</span> <span class=o>{</span>
        <span class=k>return</span> <span class=k>new</span> <span class=n>SimpleMongoClientDatabaseFactory</span><span class=o>(</span><span class=n>env</span><span class=o>.</span><span class=na>getProperty</span><span class=o>(</span><span class=n>ENTITY_MONGODB_URL</span><span class=o>));</span>
    <span class=o>}</span>
<span class=o>}</span>
</code></pre></td></tr></table></div></div><h2 id=使用spel表达式来动态获取集合的值>使用SPEL表达式来动态获取集合的值</h2><p>Spring Data MongoDB的POJO需要用<code>@Document(collection = "xxx")</code>来指明映射数据库的某个集合（相当于JPA里的<code>@Table(name = "xxx")</code>），但有时不想要直接写死集合名字，可以用SPEL表达式来实现：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt> 1
</span><span class=lnt> 2
</span><span class=lnt> 3
</span><span class=lnt> 4
</span><span class=lnt> 5
</span><span class=lnt> 6
</span><span class=lnt> 7
</span><span class=lnt> 8
</span><span class=lnt> 9
</span><span class=lnt>10
</span><span class=lnt>11
</span><span class=lnt>12
</span><span class=lnt>13
</span><span class=lnt>14
</span><span class=lnt>15
</span><span class=lnt>16
</span><span class=lnt>17
</span><span class=lnt>18
</span><span class=lnt>19
</span><span class=lnt>20
</span><span class=lnt>21
</span><span class=lnt>22
</span><span class=lnt>23
</span><span class=lnt>24
</span><span class=lnt>25
</span><span class=lnt>26
</span><span class=lnt>27
</span><span class=lnt>28
</span><span class=lnt>29
</span><span class=lnt>30
</span><span class=lnt>31
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-java data-lang=java><span class=c1>// 将集合名字作为一个变量，存到一个bean对象中
</span><span class=c1>// @Data是lombok的注解，用来自动生成setter和getter方法
</span><span class=c1></span>
<span class=nd>@Bean</span><span class=o>(</span><span class=n>name</span> <span class=o>=</span> <span class=s>&#34;entityMongoCollection&#34;</span><span class=o>)</span>
<span class=kd>public</span> <span class=n>EntityMongoCollection</span> <span class=nf>getEntityMongoCollection</span><span class=o>()</span> <span class=o>{</span>
    <span class=k>return</span> <span class=k>new</span> <span class=n>EntityMongoCollection</span><span class=o>(</span><span class=s>&#34;myCollection&#34;</span><span class=o>);</span>
<span class=o>}</span>

<span class=nd>@Data</span>
<span class=nd>@AllArgsConstructor</span>
<span class=kd>public</span> <span class=kd>class</span> <span class=nc>EntityMongoCollection</span> <span class=o>{</span>

    <span class=kd>private</span> <span class=n>String</span> <span class=n>collectionName</span><span class=o>;</span>

<span class=o>}</span>

<span class=c1>// 用SPEL表达式来获取这个bean里的变量值
</span><span class=c1></span><span class=nd>@Data</span>
<span class=nd>@Document</span><span class=o>(</span><span class=n>collection</span> <span class=o>=</span> <span class=s>&#34;#{@entityMongoCollection.getCollectionName()}&#34;</span><span class=o>)</span>
<span class=kd>public</span> <span class=kd>class</span> <span class=nc>EntityMongo</span> <span class=kd>implements</span> <span class=n>Serializable</span> <span class=o>{</span>

    <span class=nd>@Id</span>
    <span class=nd>@Field</span><span class=o>(</span><span class=s>&#34;id&#34;</span><span class=o>)</span>
    <span class=kd>private</span> <span class=n>String</span> <span class=n>id</span><span class=o>;</span>

    <span class=nd>@Field</span><span class=o>(</span><span class=s>&#34;ref_no&#34;</span><span class=o>)</span>
    <span class=kd>private</span> <span class=n>String</span> <span class=n>refNo</span><span class=o>;</span>
    
    <span class=nd>@Field</span><span class=o>(</span><span class=s>&#34;version&#34;</span><span class=o>)</span>
    <span class=kd>private</span> <span class=n>Interger</span> <span class=n>version</span><span class=o>;</span>
<span class=o>}</span>
</code></pre></td></tr></table></div></div><h2 id=查询数据库>查询数据库</h2><p>可以用官方提供的MongoTemplate来查询数据，也可以使用MongoRepository和<code>@Query</code>注解来实现：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span><span class=lnt>3
</span><span class=lnt>4
</span><span class=lnt>5
</span><span class=lnt>6
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-java data-lang=java><span class=kd>public</span> <span class=kd>interface</span> <span class=nc>EntityMongoRepository</span> <span class=kd>extends</span> <span class=n>MongoRepository</span><span class=o>&lt;</span><span class=n>EntityMongo</span><span class=o>,</span> <span class=n>String</span><span class=o>&gt;</span> <span class=o>{</span>

    <span class=nd>@Query</span><span class=o>(</span><span class=s>&#34;{&#39;refNo&#39;:?0 , &#39;version&#39;:?1}&#34;</span><span class=o>)</span>
    <span class=n>List</span><span class=o>&lt;</span><span class=n>EntityMongo</span><span class=o>&gt;</span> <span class=nf>findByRefNoAndVersion</span><span class=o>(</span><span class=kd>final</span> <span class=n>String</span> <span class=n>refNo</span><span class=o>,</span> <span class=kd>final</span> <span class=n>String</span> <span class=n>version</span><span class=o>);</span>

<span class=o>}</span>
</code></pre></td></tr></table></div></div><p>如果只需要查询部分字段，可以用MongoTemplate的Projection来实现：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span><span class=lnt>3
</span><span class=lnt>4
</span><span class=lnt>5
</span><span class=lnt>6
</span><span class=lnt>7
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-java data-lang=java><span class=n>String</span> <span class=n>collectionName</span> <span class=o>=</span> <span class=s>&#34;test&#34;</span><span class=o>;</span>

<span class=n>Query</span> <span class=n>query</span> <span class=o>=</span> <span class=k>new</span> <span class=n>Query</span><span class=o>();</span>
<span class=n>query</span><span class=o>.</span><span class=na>fields</span><span class=o>().</span><span class=na>include</span><span class=o>(</span><span class=s>&#34;ref_no&#34;</span><span class=o>);</span> <span class=c1>// 想查询的字段
</span><span class=c1></span><span class=n>query</span><span class=o>.</span><span class=na>fields</span><span class=o>().</span><span class=na>exclude</span><span class=o>(</span><span class=s>&#34;version&#34;</span><span class=o>);</span>  <span class=c1>// 不想查询的字段
</span><span class=c1></span>
<span class=kd>final</span> <span class=n>List</span><span class=o>&lt;</span><span class=n>EntityMongo</span><span class=o>&gt;</span> <span class=n>list</span> <span class=o>=</span> <span class=n>mongoTemplate</span><span class=o>.</span><span class=na>find</span><span class=o>(</span><span class=n>query</span><span class=o>,</span> <span class=n>EntityMongo</span><span class=o>.</span><span class=na>class</span><span class=o>,</span> <span class=n>collectionName</span><span class=o>);</span>
</code></pre></td></tr></table></div></div><h2 id=参考链接>参考链接</h2><ul><li><a href=https://www.jianshu.com/p/847cb7b8c458 target=_blank rel="noopener noreffer">Spring Data Mongo中@Transient无效的解决办法</a></li><li><a href=https://blog.csdn.net/zhan107876/article/details/111597156 target=_blank rel="noopener noreffer">Spring 框架 MongoDB 去掉_class属性字段</a></li><li><a href=https://stackoverflow.com/questions/58537519/mongo-date-custom-converter-not-being-called-when-save-method-of-mongo-repositor target=_blank rel="noopener noreffer">Mongo Date Custom Converter not being called when save method of mongo repository is invoked</a></li><li><a href=https://blog.csdn.net/lidai352710967/article/details/96307151 target=_blank rel="noopener noreffer">The bean &lsquo;xxxxx&rsquo;, defined in null, could not be registered. A bean with that name has already &mldr;</a></li><li><a href=https://blog.csdn.net/qq_28336351/article/details/94402768 target=_blank rel="noopener noreffer">SpringBoot整合MongoDB多数据源</a></li><li><a href=https://www.jianshu.com/p/7cdf2761a9f3 target=_blank rel="noopener noreffer">springboot整合MongoDB</a></li><li><a href=https://stackoverflow.com/questions/41698445/cannot-resolve-bean-in-spel-for-spring-data-mongodb-collection-name target=_blank rel="noopener noreffer">Cannot resolve bean in SpEL for Spring Data MongoDB collection name</a></li></ul><div class="details admonition note open"><div class="details-summary admonition-title"><i class="icon fas fa-pencil-alt fa-fwnote"></i>注意<i class="details-icon fas fa-angle-right fa-fw"></i></div><div class=details-content><div class=admonition-content>本文最后更新于 <span class=timeago datetime=2021-09-27T23:27:22 title="September 27, 2021">September 27, 2021</span>，文中内容可能已过时，请谨慎使用。</div></div></div><div class=post-reward><input type=checkbox name=reward id=reward hidden>
<label class=reward-button for=reward>赞赏支持</label><div class=qr-code><label class=qr-code-image for=reward><img class=image src=https://cdn.jsdelivr.net/gh/lewky/lewky.github.io@master/images/common/wechat.png>
<span>微信打赏</span></label>
<label class=qr-code-image for=reward><img class=image src=https://cdn.jsdelivr.net/gh/lewky/lewky.github.io@master/images/common/alipay.png>
<span>支付宝打赏</span></label></div></div></div><div class=post-footer id=post-footer><div class=post-info><div class=post-info-line><div class=post-info-mod><span>更新于 2021-09-27</span></div><div class=post-info-license><span><a rel="license external nofollow noopener noreffer" href=https://creativecommons.org/licenses/by-nc/4.0/ target=_blank>CC BY-NC 4.0</a></span></div></div><div class=post-info-line><div class=post-info-md><span><a class=link-to-markdown href=/posts/spring-data-mongodb/index.md target=_blank>阅读原始文档</a></span></div><div class=post-info-share><span><a href=javascript:void(0); title="分享到 Twitter" data-sharer=twitter data-url=https://lewky.cn/posts/spring-data-mongodb/ data-title="Spring Data MongoDB问题汇总" data-hashtags=工作记录,Spring,MongoDB><i class="fab fa-twitter fa-fw"></i></a><a href=javascript:void(0); title="分享到 Facebook" data-sharer=facebook data-url=https://lewky.cn/posts/spring-data-mongodb/ data-hashtag=工作记录><i class="fab fa-facebook-square fa-fw"></i></a><a href=javascript:void(0); title="分享到 Hacker News" data-sharer=hackernews data-url=https://lewky.cn/posts/spring-data-mongodb/ data-title="Spring Data MongoDB问题汇总"><i class="fab fa-hacker-news fa-fw"></i></a><a href=javascript:void(0); title="分享到 Line" data-sharer=line data-url=https://lewky.cn/posts/spring-data-mongodb/ data-title="Spring Data MongoDB问题汇总"><i data-svg-src=https://cdn.jsdelivr.net/npm/simple-icons@2.14.0/icons/line.svg></i></a><a href=javascript:void(0); title="分享到 微博" data-sharer=weibo data-url=https://lewky.cn/posts/spring-data-mongodb/ data-title="Spring Data MongoDB问题汇总"><i class="fab fa-weibo fa-fw"></i></a></span></div></div></div><div class=post-info-more><section class=post-tags><i class="fas fa-tags fa-fw"></i>&nbsp;<a href=/tags/%E5%B7%A5%E4%BD%9C%E8%AE%B0%E5%BD%95/>工作记录</a>,&nbsp;<a href=/tags/spring/>Spring</a>,&nbsp;<a href=/tags/mongodb/>MongoDB</a></section><section><span><a href=javascript:void(0); onclick=window.history.back();>返回</a></span>&nbsp;|&nbsp;<span><a href=/>主页</a></span></section></div><div class=post-nav><a href=/posts/log-framework/ class=prev rel=prev title=日志框架与门面模式><i class="fas fa-angle-left fa-fw"></i>日志框架与门面模式</a>
<a href=/posts/mysql-issues/ class=next rel=next title=MySQL问题汇总>MySQL问题汇总<i class="fas fa-angle-right fa-fw"></i></a></div></div><div id=comments><div id=waline></div><script src=https://cdn.jsdelivr.net/npm/@waline/client/dist/Waline.min.js></script><script>new Waline({el:'#waline',meta:["nick","mail","link"],requiredMeta:["nick","mail"],login:"force",placeholder:"为防恶意灌水攻击，评论前需注册并登录，望见谅~",serverURL:"https://comment.lewky.cn/",avatarCDN:"https://sdn.geekzu.org/avatar/",pageSize:20,avatar:"retro",lang:"zh-CN",visitor:true,highlight:true,uploadImage:false,emoji:['https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/嘉然今天吃什么','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/大航海嘉然','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/向晚大魔王','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/贝拉kira','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/珈乐Carol','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/乃琳Queen','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/EveOneCat','https://cdn.jsdelivr.net/gh/walinejs/emojis@1.0.0/weibo','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/滑稽','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/default']});</script></div></article></div></main><footer class=footer><div class=footer-container><div class=footer-line><span id=run-time></span>|
<a href=http://rssblog.vercel.app/ target=_blank rel=noopener title=RSSBlog><i class="fas fa-fw fa-inbox"></i>&nbsp;RSSBlog</a></div><div class=footer-line><i class="far fa-copyright fa-fw"></i><span itemprop=copyrightYear>2018 - 2022</span><span class=author itemprop=copyrightHolder>&nbsp;<a href=/ target=_blank>雨临Lewis</a></span>&nbsp;|&nbsp;<span class=license><a rel="license external nofollow noopener noreffer" href=https://creativecommons.org/licenses/by-nc/4.0/ target=_blank>CC BY-NC 4.0</a></span><span class=icp-splitter>&nbsp;|&nbsp;</span><br class=icp-br><span class=icp><a target=_blank href=http://www.beian.miit.gov.cn/ style=font-weight:700>粤ICP备19103822</a></span></div></div></footer></div><div id=fixed-buttons><a href=# id=back-to-top class=fixed-button title=回到顶部><i class="fas fa-arrow-up fa-fw"></i></a><a href=# id=view-comments class=fixed-button title=查看评论><i class="fas fa-comment fa-fw"></i></a></div><div class=sidebar_wo><div id=leimu><img src=https://cdn.jsdelivr.net/gh/lewky/lewky.github.io@master/images/b2t/leimuA.png alt=雷姆 onmouseover="this.src='https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/b2t/leimuB.png'" onmouseout="this.src='https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/b2t/leimuA.png'" title=回到顶部></div><div class=sidebar_wo id=lamu><img src=https://cdn.jsdelivr.net/gh/lewky/lewky.github.io@master/images/b2t/lamuA.png alt=雷姆 onmouseover="this.src='https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/b2t/lamuB.png'" onmouseout="this.src='https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/b2t/lamuA.png'" title=回到底部></div></div><link rel=stylesheet href=https://cdn.jsdelivr.net/npm/lightgallery.js@1.2.0/dist/css/lightgallery.min.css><script type=text/javascript src=https://cdn.jsdelivr.net/npm/smooth-scroll@16.1.3/dist/smooth-scroll.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/lazysizes@5.2.2/lazysizes.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/lightgallery.js@1.2.0/dist/js/lightgallery.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/lg-thumbnail.js@1.2.0/dist/lg-thumbnail.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/lg-zoom.js@1.2.0/dist/lg-zoom.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/sharer.js@0.4.0/sharer.min.js></script><script type=text/javascript>window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":30},"comment":{},"lightGallery":{"actualSize":false,"exThumbImage":"data-thumbnail","hideBarsDelay":2000,"selector":".lightgallery","speed":400,"thumbContHeight":80,"thumbWidth":80,"thumbnail":true}};</script><script type=text/javascript src=/js/theme.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/jquery@2.1.3/dist/jquery.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/jquery-backstretch@2.1.18/jquery.backstretch.min.js></script><script>var $cdnPrefix="https://cdn.jsdelivr.net/gh/lewky/lewky.github.io@master";</script><script type=text/javascript src=/js/custom.js></script><script>var _hmt=_hmt||[];(function(){var hm=document.createElement("script");hm.src="https://hm.baidu.com/hm.js?6f278ed0fdb01edb3b1e7398379e5432";var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(hm,s);})();</script></body></html>